package com.witmed.lib_base

import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.ToastUtils
import com.witmed.IViewModel
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable

abstract class BaseViewModel<out M:BaseModel>(): ViewModel(),IViewModel {
    protected lateinit var m: @UnsafeVariance M
//    abstract fun <M> createModel(): M
    abstract fun createModel(): M

//    private var uiChangeListener: UIChangeListener? = null
    lateinit var uiChangeListener: UIChangeListener
    lateinit var disposable:CompositeDisposable
    override fun onCreate() {
        m=createModel()
        initData()
    }

    abstract fun initData()

    override fun onResume() {

    }

    //使用rxjava提供的CompositeDisposable记录绑定的异步事件
    fun addCompositeDisposable(dispos: Disposable){
        if(!this::disposable.isInitialized){
            disposable=CompositeDisposable()
        }
        disposable.add(dispos)
    }

    //获取viewmodel中数据事件驱动器
    open fun getUiChangedListener(): UIChangeListener? {
        if (!this::uiChangeListener.isInitialized)
            uiChangeListener = UIChangeListener()
        return uiChangeListener
    }

    fun showDialog(){
        //时间戳：保证Livedata下面的数据值每一次都发送变化，进而使观察者监听到数据改变
        uiChangeListener?.getShowDialogData()?.value=System.currentTimeMillis().toString()

    }

    fun hiddenDialog(){
        uiChangeListener?.getHiddenDialogData()?.value=System.currentTimeMillis().toString()
    }

    //跳转
    fun startActivity(map:Map<String,Any>){
        uiChangeListener?.getStartActivityData()?.value=map
    }


    fun closeActivity(){
        uiChangeListener?.getCloseActivityData()?.value=System.currentTimeMillis().toString()
    }

    fun showMsg(msg:String){
        val s = StringUtils.null2Length0(msg)//如果msg==null,返回“”
        ToastUtils.showLong(s)
    }


    override fun onDestory() {
        if(disposable!=null){
            disposable.dispose()
            disposable.clear()
        }
        if(m!=null){
            m.cleared()
        }
    }

    //基于LiveData事件驱动内部类
    class UIChangeListener{
        //跳转
         var startActivity:BaseLiveData<Map<String,Any>>?=null
        //关闭Activity
        var closeActivity:BaseLiveData<String>?=null
        //显示对话框
        var showDialog:BaseLiveData<String>?=null
        //隐藏对话框
        var hiddenDialog:BaseLiveData<String>?=null


        fun getStartActivityData():BaseLiveData<Map<String,Any>>{
            if (startActivity == null) startActivity = BaseLiveData()
            return startActivity!!
        }

        fun getCloseActivityData():BaseLiveData<String>{
            if (closeActivity == null) closeActivity = BaseLiveData()
            return closeActivity!!
        }


        fun getShowDialogData():BaseLiveData<String>{
            if (showDialog == null) showDialog = BaseLiveData()
            return showDialog!!
        }

        fun getHiddenDialogData():BaseLiveData<String>{
            if (hiddenDialog == null) hiddenDialog = BaseLiveData()
            return hiddenDialog!!
        }
    }
}